#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "UI/MainUI/CMainUIManage.h"
#include "UI/ModRun/Output/FactoryOutput.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QIcon* icon = new QIcon(":/Resource/Mod.ico");
    setWindowIcon(*icon);

    //绑定页面切换信号;
    connect(ui->tab_Main, &QTabWidget::currentChanged, this, &MainWindow::tabMain_currentChanged);

    //绑定清空按钮信号;
    //connect(ui->btn_Clear, &QPushButton::clicked, this, &MainWindow::ClearLog);
    connect(ui->btn_Clear, &QPushButton::clicked, ui->ed_Log, &QTextEdit::clear);

    //绑定运行按钮信号;
    connect(ui->btn_ModRun, &QPushButton::clicked, this, &MainWindow::OnModelRun);

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //绑定模式按钮按下信号;
    InitClickBind();

    //初始化输出信息;
    FactoryOutput::GetInstance()->InitData(dynamic_cast<IUIElement*>(this));

    //初始化UI信息;
    CMainUIManage::GetInstance()->InitData(dynamic_cast<IUIElement *>(this));

    //设置开始显示Tab0;
    ui->tab_Main->setCurrentIndex(0);

    /////////////////////////////////////////////
    //测试：
    ui->ed_Log->setText("Abcdefg............................");
}

MainWindow::~MainWindow()
{
    CMainUIManage::Destory();

    FactoryOutput::Destory();

    delete ui;
}

//tab页签改变通知;
void MainWindow::tabMain_currentChanged(int index)
{
    if (index == 0)
    {
        CMainUIManage::GetInstance()->DisplayMain(emMainStatus_All);
    }
    else if (index == 1)
    {
        CMainUIManage::GetInstance()->DisplayMain(emMainStatus_Mod);
    }
}

//清空日志;
void MainWindow::ClearLog(bool checked/* = false*/)
{
    Q_UNUSED(checked);
    ui->ed_Log->clear();
    //ui->ed_Log->setText("");
}

//点击运行按钮;
void MainWindow::OnModelRun(bool checked/* = false*/)
{
    Q_UNUSED(checked);

    //显示运行结果;
    CMainUIManage::GetInstance()->GetCurrentMainStatus()->DisplayRun();
}

//点击了按钮;
void MainWindow::OnClicked(bool checked/* = false*/)
{
    Q_UNUSED(checked);

    QPushButton *pBtn = dynamic_cast<QPushButton *>(focusWidget());
    if (NULL != pBtn)
    {
        EnumModRun model = pBtn->property("model").value<EnumModRun>();
        qDebug() << model;
        CMainUIManage::GetInstance()->SetLastModel(model);
        ui->tab_Main->setCurrentIndex(1);
    }
}

//初始化按钮绑定;
void MainWindow::InitClickBind()
{
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //绑定模式按钮按下信号;
    //工厂模式;
    ClickBindOne(ui->btn_Factory, emModRunFactory);
    //抽象工厂模式;
    ClickBindOne(ui->btn_Factory_Abstract, emModRunFactoryAbstract);
    //观察者模式;
    ClickBindOne(ui->btn_Observer, emModRunObserver);
    //单例模式;
    ClickBindOne(ui->btn_Singleton, emModRunSingleton);
    //策略模式;
    ClickBindOne(ui->btn_Strategy, emModRunStrategy);
    //模板方法模式;
    ClickBindOne(ui->btn_Template, emModRunTemplateMethod);
    //建造者模式;
    ClickBindOne(ui->btn_Builder, emModRunBuilder);
    //原型模式;
    ClickBindOne(ui->btn_Prototype, emModRunPrototype);
    //适配器模式;
    ClickBindOne(ui->btn_Adapter, emModRunAdapter);
    //桥接模式;
    ClickBindOne(ui->btn_Bridge, emModRunBridge);
    //组合模式;
    ClickBindOne(ui->btn_Composite, emModRunComposite);
    //享元模式;
    ClickBindOne(ui->btn_Flyweight, emModRunFlyweight);
    //迭代器模式;
    ClickBindOne(ui->btn_Iterator, emModRunIterator);
    //装饰器模式;
    ClickBindOne(ui->btn_Decorator, emModRunDecorator);
    //外观模式;
    ClickBindOne(ui->btn_Facade, emModRunFacade);
    //代理模式;
    ClickBindOne(ui->btn_Proxy, emModRunProxy);
    //命令模式;
    ClickBindOne(ui->btn_Command, emModRunCommand);
    //解释器模式;
    ClickBindOne(ui->btn_Interpreter, emModRunInterpreter);
    //中介者模式;
    ClickBindOne(ui->btn_Mediator, emModRunMediator);
    //责任链模式;
    ClickBindOne(ui->btn_Chain, emModRunChain);
    //备忘录模式;
    ClickBindOne(ui->btn_Memento, emModRunMemento);
    //状态模式;
    ClickBindOne(ui->btn_State, emModRunState);
    //访问者模式;
    ClickBindOne(ui->btn_Visitor, emModRunVisitor);
}

//绑定一个按钮到ModRun;
void MainWindow::ClickBindOne(QPushButton* pBtn, EnumModRun emModRun)
{
    connect(pBtn, &QPushButton::clicked, this, &MainWindow::OnClicked);
    pBtn->setProperty("model", emModRun);

    //设置按钮样式 说明已经实现;
    //设置按钮的样式表
    pBtn->setStyleSheet(R"(
                            QPushButton {
                                color: white;
                                background-color: #56ab2f; /* 亮绿色 */
                                border-radius: 5px; /* 圆角 */
                                border: none;
                                padding: 5px;
                            }
                            QPushButton:hover {
                                background-color: #438f24; /* 暗绿色 */
                            }
                            QPushButton:pressed {
                                background-color: #2f6319; /* 暗绿色 */
                            }
                        )");
}

void MainWindow::accept(IVisitor& visitor)
{
    visitor.VisitorElement(dynamic_cast<IUIElement *>(this));
}

//获取UI;
Ui::MainWindow* MainWindow::GetUI()
{
    return ui;
}

//文字过长显示省略号;
QString MainWindow::GetElideString(const QFont& font, const QString content, const int width)
{
    QFontMetrics fontMetrics(font);
    return fontMetrics.elidedText(content, Qt::ElideRight, width);
}
